__author__ = 'Soul'

from core.JsonDataFetcher import *
from core.WebkitLoginHelper import GtkLoginForm
import string
import sys
import os
import threading
from datetime import datetime
import time
import random

class QiXinSearchPageFetcher(JsonDataFetcher):
    def init(self, url, intent, recurse=False):
        if intent:
            self.__login_url__ = intent['login_url']
            self.__redirect_url__ = intent['redirect_url']
            self.__manul_login__ = intent['manul_login']
            self.__cookie_path__ = intent['cookie']

        timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
        path = "qixinsearch_%s.csv"% (timestamp)
        self.__csv_file__ = open(path, "a+")
        return JsonDataFetcher.init(self, url, True)

    def GetStoredCookies(self):
        if not self.__cookie_path__:
            return ''
        if not os.path.exists(self.__cookie_path__):
            return ''

        cookie_file = open(self.__cookie_path__, "r")
        cookie_lines = cookie_file.readlines()
        cookie_file.close()
        cookies = []
        for cookie_line in cookie_lines:
            if cookie_line:
                cookies.append(cookie_line.strip())

        cookies_str = ";".join(cookies)
        return cookies_str

    def preFectch(self, params, headers):
        headers['Host'] = 'www.qixin.com'
        headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
        headers['Referer'] = 'http://www.qixin.com/'
        # headers['Accept-Encoding'] = 'gzip, deflate, sdch'
        headers['Accept-Language'] = 'en-US,en;q=0.8,zh-CN;q=0.6,zh-TW;q=0.4'

        if self.__manul_login__:
            gtk_login_form = GtkLoginForm(
                    self.__login_url__,
                    self.__redirect_url__,
                    self.__cookie_path__
            )
            bNeedRefresh = gtk_login_form.CheckCookie()
            if bNeedRefresh:
                gtk_login_form.Show()

            if not os.path.exists(self.__cookie_path__):
                logger.error("Error:: missing qixin cookie path!!")

            cookies = self.GetStoredCookies()
            headers['Cookie'] = cookies
            self.__random_sleep()

    def preProcess(self):
        return

    def __del__(self):
        pass

    def __random_sleep(self):
        rand_int = random.randint(0, 10)
        logger.info("Will sleep %d to start request"%(rand_int))
        time.sleep(rand_int)

    def analysis(self):
        if self.__json_data__:
            global seen_urls, urls_to_visit
            try:
                import json
                sggst_data = self.__json_data__
                if len(sggst_data):
                    for com_item in sggst_data:
                        ent_name = com_item['name']
                        ent_eid =  com_item['eid']
                        ent_real_url = "http://www.qixin.com/company/%s" % ent_eid

                        self.__csv_file__.write("%s,%s\n" % (ent_name, ent_real_url))

                        if self.__mutex__.acquire(1):
                            seen = seen_urls.__contains__(ent_real_url)
                            self.__mutex__.release()

                        if self.__mutex__.acquire(1):
                            visited = urls_to_visit.__contains__(ent_real_url)
                            self.__mutex__.release()

                        logger.info("append %s to urls_to_visit" % (ent_real_url))
                        if (not seen) and (not visited):
                            if self.__mutex__.acquire(1):
                                urls_to_visit.append({
                                    "url": ent_real_url,
                                    "controller": "QiXinDetailPageFetcher",
                                    "intent" : {
                                        'cookie_path' : self.__cookie_path__
                                    }
                                })
                                self.__mutex__.release()
                else:
                    logger.error("WoW! Total count : " + len(sggst_data))
                    logger.error("==== Maybe this account is blocked!! ====")
            except:
                (type, value, tb) = sys.exc_info()
                import traceback
                traceback.print_exception(type, value, tb)

        self.__csv_file__.close()

